﻿﻿/***************************  Ccay IGrid Editor  ***********************************/
(function(IGrid){
	var IGridManager = Ccay.UI.XControl.getIManager("XGrid");
	IGridManager._cacheRecord = IGridManager.cacheRecord;
	
	IGridManager.cacheRecord = function(type,key,record,isRemove){
		if(!this.op.cacheData || record == null)
			return this._cacheRecord(type,key,record,isRemove);

		if(!record._inserted)
			key = cacheHandler.getKeyData(this.op,record);

    	var cache = this._cacheRecord(type,key,record,isRemove);
    	//更新状态
    	cache && cacheHandler.updateStatus(this,type,cache);
    	return cache;
   }
   
   IGridManager._clearCache = IGridManager.clearCache;
   IGridManager.clearCache = function(type){
		this._clearCache(type);
		cacheHandler.updateStatus(this,type,{length:0});
		return this;
	}

   /*IGrid.Delegate.GridInit.add(function(grid,op){
	    //设置缓存数据模式
		if(op.cacheData){
			//标示缓存插件已初始化
			op._cacheData = true;
			op.cacheKey && (op._cacheKey = op.cacheKey.join('-'));
			op.handler.cacheData = $.IAdapter();
			op.handler.macthCahcheData = $.IAdapter();
			op.handler.beforeDataBind.add(cacheHandler.matchData);
			op.showStatus !== false && op.handler.endInit.add(cacheHandler.createStatusBar);
		}
		return this;
	});*/
	
	Ccay.UI.XControl.addPlugin("XGrid", "cacheData", {
	    preInit: function (grid, op) {
	    	//设置缓存数据模式
			if(op.cacheData){
				//标示缓存插件已初始化
				op._cacheData = true;
				op.cacheKey && (op._cacheKey = op.cacheKey.join('-'));
				op.handler.cacheData = $.IAdapter();
				op.handler.macthCahcheData = $.IAdapter();
				op.handler.beforeBindData.add(cacheHandler.matchData);
				op.showStatus !== false && op.handler.initComplete.add(cacheHandler.createStatusBar);
			}
			return this;
	    }
	});
	
	var cacheHandler = {
		getKeyData:function(op,record){
			var matchKeys = [];
			for(var j = 0,key;key = op.cacheKey[j];j++)
			{
				var keyData = record[key];
				if(!keyData)
					return null;
				matchKeys.push(keyData);
			}
			return matchKeys.join("-");
		},
	  	matchData:function(grid, op){
			if(op._cache && op._cache.length && op.dataset && op.dataset.length)
			{
				for(var type in op._cache)
				{
					if(type != IGrid.Const.inserted.cache)
					{
						var cacheData = op._cache[type];
						if(cacheData && cacheData.length)
						{
							for(var i = 0,recored;record = op.dataset[i];i++)
							{
								var key = cacheHandler.getKeyData(op,record);
								if(key)
								{
									var cacheRecord = cacheData[key];
									cacheRecord && !cacheRecord._inserted && op.handler.macthCahcheData.fire(type,cacheRecord,record);
								}
							}
						}
					}
				}
			}
    	}
    	,createStatusBar:function(grid, op){
	   		var bar = $.create('div','igrid-status'),html=[];
	   		html.push("<span>selected recoreds : <b class='igrid-selecteds'>0</b></span>");
	   		html.push("<span>updated recoreds : <b class='igrid-updateds'>0</b></span>");
	   		html.push("<span>inserted recoreds : <b class='igrid-addeds'>0</b></span>");
	   		html.push("<span>deleted recoreds : <b class='igrid-deleteds'>0</b></span>");

	   		bar.html(html.join('')).prependTo(grid.getBottom());
	   },
	   updateStatus:function(grid,type,cacheData){
	   		grid.op.showStatus !== false && grid.getBottom().find(' > .igrid-status > span > b' + (type ? '.igrid-' + type : '')).text(type ? cacheData.length : 0);
	   }
	}
})(Ccay.UI.IGrid);